package com.bristua.barcode.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Properties;

public class ConfReader {

	private static final String FILENAME_API = "config/api.properties";
	private static final Logger logger = LoggerFactory.getLogger(ConfReader.class);
	
	public static String getString(String key, String fileName){
		return get(key, fileName);
	}
	



	public static String getAPIString(String key){
		return get(key, FILENAME_API);
	}
	
	public static byte getAPIByte(String key){
		return Byte.valueOf(get(key, FILENAME_API));
	}
	
	public static Integer getAPIInt(String key){
		return Integer.valueOf(get(key, FILENAME_API));
	}
	

	private static String get(String key, String fileName){
		String value = null;
		FileInputStream fis = null;
		Properties props = new Properties();
		try {
			String path = ConfReader.class.getClassLoader().getResource("").toURI().getPath();
			fis = new FileInputStream(new File(path+fileName));
			props.load(fis);
			value = props.getProperty(key);
		} catch (URISyntaxException e) {
			logger.error( "读" + fileName + "属性文件发生异常",e);
		} catch (FileNotFoundException e) {
			logger.error("读" + fileName + "属性文件发生异常",e);
		} catch (IOException e) {
			logger.error( "读" + fileName + "属性文件发生异常",e);
		} finally{
			if(null != fis){
				try {
					fis.close();
				} catch (IOException e) {
				}
				fis = null;
			}
		}
		return value;
	}
}
